//package com.techsoul.module.file.controller;
//
//import com.techsoul.common.core.exception.file.FileException;
//import com.techsoul.common.core.pojo.Result;
//import com.techsoul.module.file.config.OssAliyunUtil;
//import com.techsoul.module.file.pojo.BlobUpload;
//import com.techsoul.module.file.pojo.OssCallbackResult;
//import com.techsoul.module.file.service.IOssService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.ByteArrayInputStream;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Random;
//
//
//@Api(tags = "阿里云存储")
//@RequestMapping("/aliyun/oss")
//@RestController
//public class OssController {
//    @Autowired
//    private Result result;
//
//    @Autowired
//    OssAliyunUtil aliyunOSSUtil;
//
//    @Autowired
//    private IOssService ossService;
//
//    private static ByteArrayInputStream getRandomDataStream(int length) {
//        return new ByteArrayInputStream(getRandomBuffer(length));
//    }
//
//    private static byte[] getRandomBuffer(int length) {
//        final Random randGenerator = new Random();
//        final byte[] buff = new byte[length];
//        randGenerator.nextBytes(buff);
//        return buff;
//    }
//
//    @ApiOperation(value = "oss上传签名生成")
//    @GetMapping(value = "/policy")
//    @ResponseBody
//    public Result policy() {
//        return this.result.success(ossService.policy());
//    }
//
//    @ApiOperation(value = "oss上传成功回调")
//    @PostMapping(value = "callback")
//    @ResponseBody
//    public Result callback(HttpServletRequest request) {
//        OssCallbackResult ossCallbackResult = ossService.callback(request);
//        return this.result.success(ossCallbackResult);
//    }
//
//    @ApiOperation("图片上传")
//    @RequestMapping(value = "upload", method = RequestMethod.POST)
//    public Result uploadImage(Long groupId, String id, Long uid, String fileType, int type, @RequestPart("file") MultipartFile multipartFile) {
//        List<BlobUpload> list = new ArrayList<>();
//        String name = aliyunOSSUtil.upload(multipartFile);
//        insertPic(groupId, multipartFile, name, "image");
//        BlobUpload blobUploadEntity = new BlobUpload();
//        blobUploadEntity.setFileName(multipartFile.getOriginalFilename());
//        blobUploadEntity.setFileUrl(name);
//        blobUploadEntity.setThumbnailUrl(name);
//        list.add(blobUploadEntity);
//        return this.result.success(list);
//    }
//
//    @ApiOperation("上传")
//    @RequestMapping(value = "/uploads", method = RequestMethod.POST)
//    public Result uploadImages(Long groupId, Long id, int type, @RequestPart("file") MultipartFile[] multipartFile) {
//        List<BlobUpload> list = new ArrayList<>();
//        if (multipartFile != null && multipartFile.length > 0) {
//            for (int i = 0; i < multipartFile.length; i++) {
//                String name = aliyunOSSUtil.upload(multipartFile[i]);
//                insertPic(groupId, multipartFile[i], name, "image");
//                BlobUpload blobUploadEntity = new BlobUpload();
//                blobUploadEntity.setFileName(multipartFile[i].getOriginalFilename());
//                blobUploadEntity.setFileUrl(name);
//                blobUploadEntity.setThumbnailUrl(name);
//                list.add(blobUploadEntity);
//            }
//        }
//        return this.result.success(list);
//    }
//
//
//    @ApiOperation("图片上传")
//    @RequestMapping(value = "uploadImage", method = RequestMethod.POST)
//    public Result uploadImages2(Long groupId, @RequestPart("file") MultipartFile[] multipartFile) {
//        List<BlobUpload> list = new ArrayList<>();
//        if (multipartFile != null && multipartFile.length > 0) {
//            for (int i = 0; i < multipartFile.length; i++) {
//                String name = aliyunOSSUtil.upload(multipartFile[i]);
//                insertPic(groupId, multipartFile[i], name, "image");
//                BlobUpload blobUploadEntity = new BlobUpload();
//                blobUploadEntity.setFileName(multipartFile[i].getOriginalFilename());
//                blobUploadEntity.setFileUrl(name);
//                blobUploadEntity.setThumbnailUrl(name);
//                list.add(blobUploadEntity);
//            }
//        }
//        return this.result.success(list);
//    }
//
//    @ApiOperation("上传文件")
//    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
//    public Result uploadFile(Long groupId, String id, Long uid, String fileType, int type, @RequestPart("file") MultipartFile multipartFile) {
//        List<BlobUpload> list = new ArrayList<>();
//        String fileExtension = getFileExtension(multipartFile.getOriginalFilename()).toLowerCase();
//        if (multipartFile != null) {
//            if (!(fileType.equals("file") || fileType.equals("video"))) {
//                throw new FileException("文件类型错误");
//            }
//            if (fileType.equals("file")) {
//                if (!fileExtension.equals(".csv")) {
//                    throw new FileException("文件格式错误");
//                }
//            }
//            if (fileType.equals("video")) {
//                if (!fileExtension.equals(".mp4")) {
//                    throw new FileException("视频格式错误");
//                }
//            }
//            String name = aliyunOSSUtil.upload(multipartFile);
//            return this.result.success(insertPic(groupId, multipartFile, name, fileType));
//        }
//        return this.result.success(list);
//    }
//
//
//    @PostMapping("/upload1")
//    @ApiOperation("上传文件")
//    public Result upload(@RequestParam("file") MultipartFile file) throws Exception {
//        return this.result.success(aliyunOSSUtil.upload(file));
//    }
//
//    @PostMapping("/uploads1")
//    @ApiOperation("多文件上传文件")
//    public Result uploads(@RequestPart("file") MultipartFile[] file) throws Exception {
//        StringBuffer stringBuffer = new StringBuffer();
//        if (file != null && file.length > 0) {
//            for (int i = 0; i < file.length; i++) {
//                stringBuffer.append(aliyunOSSUtil.upload(file[i]) + ",");
//            }
//        }
//        return this.result.success(stringBuffer);
//    }
//
//    private String getFileExtension(String fileName) {
//        int position = fileName.indexOf('.');
//        if (position > 0) {
//            String temp = fileName.substring(position);
//            return temp;
//        }
//        return "";
//    }
//
//    private Object insertPic(Long groupId, @RequestPart("file") MultipartFile multipartFile, String name, String type) {
//        return null;
//    }
//
////    private PmsAlbumPic insertPic(Long groupId, @RequestPart("file") MultipartFile multipartFile, String name, String type) {
////        PmsAlbumPic attachment = new PmsAlbumPic();
////        if (ValidatorUtils.notEmpty(UserUtils.getCurrentMember())) {
////            attachment.setUserId(UserUtils.getCurrentMember().getId());
////        }
////        attachment.setAlbumId(groupId);
////        attachment.setCreateTime(new Date());
////        attachment.setName(multipartFile.getOriginalFilename());
////
////        attachment.setType(type);
////        attachment.setPic(name);
////        albumPicMapper.insert(attachment);
////        return attachment;
////    }
//}
